import os,shutil,sys
f1 = open(os.getcwd()+"\\dmzj_info_yuri3.txt","r",encoding="utf-8")
FolderLines = f1.readlines()
f1.close()
FolderList = []
for line in FolderLines:
    FolderList.append(line[:line.find(" ")]+" "+line[line.find("標題_")+3:line.find(" 作者")])
    os.mkdir(line[:line.find(" ")]+" "+line[line.find("標題_")+3:line.find(" 作者")])
dirs = os.listdir(os.getcwd())
DownloadedList = []
for folder in FolderList:
    BatCommands = []
    for dir in dirs:
        if "zip" in dir and dir[:dir.find("_")] == folder[:folder.find(" ")]:
            print(dir+" match")
            shutil.move(os.getcwd()+"\\"+dir,os.getcwd()+"\\"+folder[:len(folder)-1])
    files = os.listdir(os.getcwd()+"\\"+folder[:len(folder)])
    for f in files:
        if f[len(f)-4:]==".zip":
            print("unzip "+f)
            UnzipCommand = "7z x "+f+" -o"+f[:len(f)-4]+" -aos"
            BatCommands.append(UnzipCommand)
            DownloadedList.append(f[:f.find("_")])
            continue
    with open(os.getcwd()+"\\"+folder+"\\7zipCommand.bat","w",encoding="utf-8") as f2:
        for line in BatCommands:
            f2.write(line+"\n")
        f2.close()
    continue
with open(os.getcwd()+"\\"+folder+"\\7zipCommand.bat","w",encoding="utf-8") as f3:
        for line in DownloadedList:
            f3.write(line+"\n")
        f3.close()